package com.atguigu.gmall.index.controller;

import com.atguigu.core.bean.Resp;
import com.atguigu.gmall.index.service.IndexService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IndexService indexService;

    /**
     * 查询商品一级分类
     * @return
     */
    @GetMapping("/cates")
    public Resp<List<CategoryEntity>> queryLevel1Category() {
        List<CategoryEntity> categoryEntityList = indexService.queryLevel1Category();
        return Resp.ok(categoryEntityList);
    }

    /**
     * 通过一级分类id查询二级分类及其子类
     * @param pid
     * @return
     */
    @GetMapping("cates/{pid}")
    public Resp<List<CategoryEntity>> querySubCategories(@PathVariable("pid")Long pid){

        List<CategoryEntity> categoryEntities = this.indexService.querySubCategories(pid);

        return Resp.ok(categoryEntities);
    }


    /**
     * 分布式锁测试用例
     * @return
     */
    @GetMapping("testlock")
    public Resp<Object> testLock() throws InterruptedException {
        indexService.testLock();

        return Resp.ok(null);
    }

}
